package huoye.shops.huoye.operation.adapter;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.codeideology.android.widget.recycler.RecyclerViewAdapter;

import butterknife.BindView;
import huoye.shops.R;
import huoye.shops.huoye.operation.model.ManageEventData;
import huoye.shops.widget.RoundImageView;

import static huoye.shops.huoye.operation.fragment.ManageEventFragment.TYPE_MANAGE_EVENT_DNS;
import static huoye.shops.huoye.operation.fragment.ManageEventFragment.TYPE_MANAGE_EVENT_START;


/**
 * Created by uesr on 2018/8/10.
 */

public class
ManageEventAdapter extends RecyclerViewAdapter<ManageEventAdapter.ViewHolder, ManageEventData> {


    public ManageEventAdapter(Context context) {
        super(context, R.layout.item_manage_event);

    }

    @Override
    protected ViewHolder initViewHolder(View view, int i) {

        return new ViewHolder(view);
    }

    @Override
    protected void bindView(ViewHolder holder, final ManageEventData manageEventData, int i) {

        if (manageEventData.getiState() == TYPE_MANAGE_EVENT_DNS) {
            holder.btnJoin.setVisibility(View.GONE);
            holder.tvEventStare.setText("未开始");
        } else if (manageEventData.getiState() == TYPE_MANAGE_EVENT_START) {
            holder.tvEventStare.setSelected(false);
            holder.tvEventStare.setText("活动进行中");
            if (manageEventData.isJoin() || manageEventData.isNaming()) {
                holder.btnJoin.setText("详情");
            } else {
                holder.btnJoin.setText("参加");
            }
        } else {
            holder.tvEventStare.setSelected(true);
            holder.tvEventStare.setText("已结束");
            holder.btnJoin.setText("详情");
        }

        holder.tvEventName.setText(manageEventData.getEtClassName());

        if (manageEventData.getEtClassCode().equals("gxjc")) {
            holder.rvHead.setImageResource(R.mipmap.ic_event_gxjc);
        } else {
            holder.rvHead.setImageResource(R.mipmap.ic_event_qmccl);
        }
        holder.tvEventTime.setText("活动期限" + manageEventData.getEndTime());


        holder.btnJoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (manageEventData.getiState() == TYPE_MANAGE_EVENT_START ) {
                    //进行判断是参加还是详情
                    if (manageEventData.isJoin() || manageEventData.isNaming()) {
                        listener.onDetailsClick(manageEventData);
                    } else {
                        listener.onJoinClick(manageEventData);
                    }
                } else {
                    listener.onDetailsClick(manageEventData);
                }


            }
        });


        holder.tvEventExplain.setText(manageEventData.getRemarks());

    }


    public class ViewHolder extends RecyclerViewAdapter.ViewHolder {
        @BindView(R.id.item_manage_event_rv)
        RoundImageView rvHead;


        @BindView(R.id.item_manage_event_tv_event_name)
        TextView tvEventName;

        @BindView(R.id.item_manage_event_tv_event_state)
        TextView tvEventStare;

        @BindView(R.id.item_manage_event_tv_event_explain)
        TextView tvEventExplain;

        @BindView(R.id.item_manage_event_tv_event_time)
        TextView tvEventTime;


        @BindView(R.id.item_manage_event_btn_join)
        Button btnJoin;


        public ViewHolder(View itemView) {
            super(itemView);
        }
    }


    public void setOnManageEventDataListener(OnManageEventDataListener onManageEventDataListener) {
        this.listener = onManageEventDataListener;
    }

    private OnManageEventDataListener listener;

    public interface OnManageEventDataListener {
        void onJoinClick(ManageEventData ManageEventData);

        void onDetailsClick(ManageEventData ManageEventData);

    }

}
